package project.newCode.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JSlider;
import javax.swing.JScrollBar;
import javax.swing.JSeparator;
import javax.swing.border.TitledBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.UIManager;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import project.newCode.testRenderer;
import java.awt.event.MouseAdapter;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.JToggleButton;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import project.newCode.Acceleration;

public class Guipanel extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextPane txtpnGfds;
	private testRenderer testRenderer;
	private int SWIDTH;
	private int SHEIGHT;
	private JCheckBox chckbxRelighting;
	private JTextPane txtpnFdsq;
	private JToggleButton tglbtnIntersections;


	/**
	 * Create the frame.
	 */
	public Guipanel() {
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		SWIDTH = screenSize.width;
		SHEIGHT = screenSize.height;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 452, 417);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnRaytracer = new JMenu("Raytracer");
		menuBar.add(mnRaytracer);

		JMenuItem mntmQuit = new JMenuItem("Quit");
		mntmQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		JMenuItem mntmOpen = new JMenuItem("Open SDL file");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String wd = "/Users/roel/Downloads/practicum1_linux/XML";
				JFileChooser fc = new JFileChooser(wd);
				int rc = fc.showDialog(null, "Open SDL file");
				if (rc == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					String filename = file.getAbsolutePath();
					testRenderer.setFilestring(filename);
					txtpnFdsq.setText(file.getName());
				}
				else
					return;
			}
		});
		JMenuItem mntmSave = new JMenuItem("Save image");
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String wd = "/Users/roel/Desktop";
				JFileChooser fc = new JFileChooser(wd);
				int rc = fc.showDialog(null, "Save as");
				if (rc == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					String filename = file.getAbsolutePath();
					testRenderer.saveImage(filename);
				}
				else
					return;
			}
		});
		JMenuItem mntmMovie = new JMenuItem("Movie");
		mntmMovie.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				testRenderer.createMovie(4);
			}
		});
		mnRaytracer.add(mntmQuit);
		mnRaytracer.add(mntmOpen);
		mnRaytracer.add(mntmSave);
		mnRaytracer.add(mntmMovie);
		
		JMenuItem mntmPrintBbhierarchy = new JMenuItem("Print BBHierarchy");
		mntmPrintBbhierarchy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				testRenderer.printBBHierarchy();
			}
		});
		mnRaytracer.add(mntmPrintBbhierarchy);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);


		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Resolution", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(6, 265, 235, 102);
		contentPane.add(panel);

		JSlider slider = new JSlider();
		slider.setMinorTickSpacing(50);
		slider.setSnapToTicks(true);
		slider.setPaintTicks(true);
		slider.setMajorTickSpacing(100);

		panel.add(slider);
		slider.setMaximum(1200);
		slider.setMinimum(100);
		slider.setValue(500);

		txtpnGfds = new JTextPane();
		txtpnGfds.setBackground(UIManager.getColor("Button.background"));
		txtpnGfds.setText("500 x 500");
		panel.add(txtpnGfds);

		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				JSlider s = (JSlider) arg0.getSource();
				txtpnGfds.setText(s.getValue() + " x " + s.getValue());
				testRenderer.setResolution(s.getValue());
			}
		});

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Quality parameters", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(6, 159, 235, 94);
		contentPane.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));

		textField = new JTextField();
		textField.setText("4");
		textField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				JTextField t = (JTextField) arg0.getSource();
				testRenderer.setAntiAliasing(Integer.parseInt(t.getText()));
			}
		});
		panel_1.add(textField);
		textField.setColumns(10);

		JTextPane txtpnAntialiasing = new JTextPane();
		txtpnAntialiasing.setBackground(UIManager.getColor("Button.background"));
		panel_1.add(txtpnAntialiasing);
		txtpnAntialiasing.setText("AntiAliasing");

		textField_1 = new JTextField();
		textField_1.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				JTextField t = (JTextField) e.getSource();
				testRenderer.setBlocking(Integer.parseInt(t.getText()));
			}
		});
		panel_1.add(textField_1);
		textField_1.setText("1");
		textField_1.setColumns(10);

		JTextPane txtpnBlocking = new JTextPane();
		txtpnBlocking.setBackground(UIManager.getColor("Button.background"));
		panel_1.add(txtpnBlocking);
		txtpnBlocking.setText("Blocking");

		JLabel label = new JLabel("");
		panel_1.add(label);

		JLabel label_1 = new JLabel("");
		panel_1.add(label_1);

		JButton btnLights = new JButton("Lights");
		btnLights.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JFrame frame = new Lightpanel(testRenderer);
				frame.setBounds(SWIDTH-frame.getWidth(), SHEIGHT-frame.getHeight(), frame.getWidth(), frame.getHeight());
				frame.setVisible(true);
			}
		});
		btnLights.setBounds(157, 53, 117, 29);
		contentPane.add(btnLights);

		chckbxRelighting = new JCheckBox("Relighting");
		chckbxRelighting.setSelected(false);
		chckbxRelighting.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JCheckBox j = (JCheckBox) e.getSource();
				testRenderer.setRelighting(j.isSelected());
			}
		});
		chckbxRelighting.setBounds(6, 124, 128, 23);
		contentPane.add(chckbxRelighting);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Threads", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(375, 10, 71, 221);
		contentPane.add(panel_2);

		JSlider slider_1 = new JSlider();

		slider_1.setSnapToTicks(true);
		slider_1.setValue(6);
		slider_1.setMinimum(1);
		slider_1.setMaximum(8);
		slider_1.setMajorTickSpacing(1);
		slider_1.setPaintLabels(true);
		slider_1.setPaintTicks(true);
		slider_1.setOrientation(SwingConstants.VERTICAL);
		panel_2.add(slider_1);
		
		tglbtnIntersections = new JToggleButton("Intersections");
		tglbtnIntersections.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				JToggleButton j = (JToggleButton) arg0.getSource();
				testRenderer.setIntersectionmode(j.isSelected());
				testRenderer.repaint();
			}
		});
		tglbtnIntersections.setBounds(157, 84, 117, 29);
		contentPane.add(tglbtnIntersections);
		JButton btnRender = new JButton("Render");
		btnRender.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				tglbtnIntersections.setSelected(false);
				testRenderer.timePerformance();
			}
		});
		btnRender.setBounds(6, 10, 139, 38);
		contentPane.add(btnRender);
		
		JButton btnRefreshSdl = new JButton("Refresh SDL");
		btnRefreshSdl.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				testRenderer.setFilestring(testRenderer.getFilestring());
			}
		});
		btnRefreshSdl.setBounds(6, 53, 139, 29);
		contentPane.add(btnRefreshSdl);
		
		JComboBox comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JComboBox j = (JComboBox) arg0.getSource();
				testRenderer.setAcceleration((Acceleration) j.getItemAt(j.getSelectedIndex()));
			}
		});

		comboBox.setModel(new DefaultComboBoxModel(Acceleration.values()));
		comboBox.setBounds(6, 72, 139, 52);
		contentPane.add(comboBox);
		
		slider_1.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JSlider s = (JSlider) e.getSource();
				testRenderer.setNthreads((int) s.getValue());
			}
		});
	}

	public Guipanel(testRenderer testRenderer) {
		this();
		this.testRenderer = testRenderer;
		txtpnFdsq = new JTextPane();
		txtpnFdsq.setText(new File(testRenderer.getFilestring()).getName());
		txtpnFdsq.setBounds(6, 257, 318, 16);
		contentPane.add(txtpnFdsq);
		this.setLocation(SWIDTH -this.getWidth(), 0);
	}
}
